package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.setMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class setMealServiceImpl implements SetMealService {

    @Autowired
    SetMealMapper setMealMapper;
    @Autowired
    setMealDishMapper setMealDishMapper;
    @Autowired
    DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        //1.setmeal表操作：setmealDTO属性复制到setmeal中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //添加套餐
        setMealMapper.saveMeal(setmeal);
        //meal_dish表操作，添加套餐中的菜品
        //获取insert语句中生成的主键值
        Long setmealId = setmeal.getId();
        //获取套餐中菜品的集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //菜品集合的非空和有值判定
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.stream().forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //向套餐菜品表中插入菜品
            setMealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //调用持久层方法得到Page对象
        Page<SetmealVO> page = setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断是否为空，为空则不能删除
        if (ids == null || ids.size() == 0) {
            throw new DeletionNotAllowedException(MessageConstant.DELETE_ON_SALE);
        }
        for (Long id : ids) {
            //根据id查询套餐，起售中的套餐不能删除
            Setmeal setmeal = setMealMapper.getSetMealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            } else {
                //循环删除套餐
                setMealMapper.deleteById(id);
                //循环删除套餐关联的菜品
                setMealDishMapper.deleteById(id);
            }
        }
    }

    /**
     * 根据套餐id查询套餐
     *
     * @param
     * @return
     */
    @Override
    @Transactional
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //获取套餐信息
        Setmeal setMeal = setMealMapper.getSetMealById(id);
        BeanUtils.copyProperties(setMeal, setmealVO);
        //获取套餐中的菜品信息
        List<SetmealDish> dishes = setMealDishMapper.getDishBySetMealId(id);
        setmealVO.setSetmealDishes(dishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //1.修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.update(setmeal);
        //2.根据套餐Id删除套餐中的菜品
        setMealDishMapper.deleteById(setmealDTO.getId());
        //3.重新插入菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //4.循环添加菜品
        for (SetmealDish setmealDish : setmealDishes) {
            //把id循环添加到每个菜品中
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        //把菜品数据循环插入到套餐菜品表中
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐的起售停售
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    @Transactional
    public void StartOrStop(Integer status, Long id) {
        //如果套餐内包含停售的菜品，则不能起售
        if (status == StatusConstant.ENABLE) {
            //根据套餐的id查套餐的里菜品的状态
            List<Dish> dishList = setMealDishMapper.getDishBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                for (Dish dish : dishList) {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED + MessageConstant.STOP_DISH_NAME + dish.getName());
                    }
                }
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setMealMapper.update(setmeal);
    }

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}















